<?php echo $this->element('displayHeader'); ?>
<div class="toolbar"><?php echo $this->element('toolbar', array('buttons' => array('add', 'search', 'refresh'))); ?></div>

<?php if (empty($this->data)) : // ถ้าไม่มีข้อมูล ?>
    <?php echo $this->element('dataNotFound'); ?>
<? else : // ถ้ามีข้อมูล ?>
    <?php $paginator->options(array('update' => 'content', 'indicator' => 'indicator')); ?>
    <?php echo $this->element('dataCaptionLimit'); ?>
    <?php $cur = $paginator->counter(array('format' => '%start%')); ?>

<table class="data">
    <thead>
        <tr>
            <th class="no"><?php echo $paginator->sort('No', 'id'); ?></th>
            <th><?php echo $paginator->sort('Dispatch no', 'Dispatch.dispatch_no'); ?></th>
            <th width="100px"><?php echo $paginator->sort('Dispatch date', 'Dispatch.dispatch_date'); ?></th>
            <th><?php echo $paginator->sort('Customer', 'Customer.name'); ?></th>
            <th><?php echo $paginator->sort('Site', 'Site.name'); ?></th>
            <th width="120px"><?php echo $paginator->sort('Delivered To', 'Dispatch.deliveree'); ?></th>
            <th width="150px"><?php echo $paginator->sort('Dispatch Status', 'DispatchStatus.title'); ?></th>
            <th class="collector"><?php echo $paginator->sort('Collector', 'User.name'); ?></th>
            <th class="action">&nbsp;</th>
        </tr>
    </thead>
    <tbody>
            <?php foreach($this->data as $key => $data): ?>
                <?php
                $tableCell = array();
                $tableCell[] = $cur++;
                $tableCell[] = $data['Dispatch']['dispatch_no'];
                $tableCell[] = $data['Dispatch']['dispatch_date'];
                $tableCell[] = $data['Customer']['name'];
                $tableCell[] = $data['Site']['name'];
                $tableCell[] = $data['Dispatch']['deliveree'];

                if ($data['Dispatch']['is_dispatch_return'] == true) {
                    $tableCell[] = $data['DispatchStatus']['title'] . '&nbsp;' .
                            $html->link($html->image('icon/return.png'),
                            '/dispatch_returns/view/return_dispatch_id:' . $data['Dispatch']['id'] . '/' . strtotime('now'),
                            array('target' => '_blank', 'title' => 'This Dispatch is already returned.'), null, false);
                } elseif ($data['Dispatch']['dispatch_status_id'] == DispatchStatus::ON_DISPATCH_STATUS || $data['Dispatch']['dispatch_status_id'] == DispatchStatus::PARTIAL_CONSIGNED_STATUS) {
                    $tableCell[] = $data['DispatchStatus']['title'] . '&nbsp;' .
                            $html->link($html->image('icon/return2.png'),
                            '/dispatch_returns/view/dispatch_id:' . $data['Dispatch']['id'],
                            array('target' => '_blank', 'title' => 'Return this Dispatch'), null, false);
                } else {
                    $tableCell[] = $data['DispatchStatus']['title'];
                }

                $tableCell[] = $data['User']['name'];
                $tableCell[] =
                        // edit button
                        $ajax->link(
                        $html->image('icon/edit.png', array('title' => 'edit', 'alt' => 'edit')),
                        'view/' . $data['Dispatch']['id'],
                        array('indicator' => 'indicator', 'update' => 'content'),   // เปลี่ยนหน้า
                        null, false
                );
                echo $html->tableCells($tableCell, array('class' => 'odd-cell'), array('class' => 'even-cell'));
                ?>
            <?php endforeach; ?>
    </tbody>
</table>

    <?php echo $this->element('paging'); ?>
<? endif; ?>